package bist.believeinstep.impl;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import bist.believeinstep.dao.ConnectionDAO;
import bist.believeinstep.dao.TourBookingDAO;
import bist.believeinstep.domain.TourBooking;
import bist.believeinstep.mapper.TourBookingMapper;

public class TourBookingTemplate implements TourBookingDAO, ConnectionDAO {
	
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public void setDataSource(DataSource datasource){
		this.jdbcTemplate = new JdbcTemplate(datasource);
	}
	@Override
	public void create(TourBooking tourBooking){
		String query = "INSERT INTO bist_tour_bookings"
				+ "(tour_booking_code,total_booking,adult_booking, children_booking, baby_booking"
				+ "adult_price, children_price, baby_price"
				+ "state, created_date, modified_date, created_by, modified_by, bist_users_is,"
				+ "bist_tours_is, bist_contact_id) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		try{
			jdbcTemplate.update(query, new Object[]{
				tourBooking.getCode(),
				tourBooking.getTotalBooking(),
				tourBooking.getAdultBooking(),
				tourBooking.getChildrenBooking(),
				tourBooking.getAdultPrice(),
				tourBooking.getChildrenPrice(),
				tourBooking.getBabyPrice(),
				tourBooking.getState(),
				tourBooking.getCreated(),
				tourBooking.getModified(),
				tourBooking.getCreatedBy(),
				tourBooking.getModifiedBy(),
				tourBooking.getBistUserId(),
				tourBooking.getBistTourId(),
				tourBooking.getBistContactId()
			});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void update(TourBooking tourBooking){
		String query = "UPDATE bist_tour_bookings"
				+ "SET tour_booking_code = ?"
				+ "total_booking = ?"
				+ "adult_booking = ?"
				+ "children_booking = ?"
				+ "baby_booking = ?"
				+ "adult_price = ?"
				+ "children_price = ?"
				+ "baby_price = ?"
				+ "state = ?"
				+ "created_date = ?"
				+ "modified_date = ?"
				+ "created_by = ?"
				+ "modified_by = ?"
				+ "bist_users_id = ?"
				+ "bist_tours_id = ?"
				+ "bist_contacts_id = ?";
		try{
			jdbcTemplate.update(query, new Object[]{
				tourBooking.getCode(),
				tourBooking.getTotalBooking(),
				tourBooking.getAdultBooking(),
				tourBooking.getChildrenBooking(),
				tourBooking.getAdultPrice(),
				tourBooking.getChildrenPrice(),
				tourBooking.getBabyPrice(),
				tourBooking.getState(),
				tourBooking.getCreated(),
				tourBooking.getModified(),
				tourBooking.getCreatedBy(),
				tourBooking.getModifiedBy(),
				tourBooking.getBistUserId(),
				tourBooking.getBistTourId(),
				tourBooking.getBistContactId()		
			});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void delete(Integer id){
		String query = "DELETE FROM bist_tour_bookings WHERE id = ?";
		try{
			jdbcTemplate.update(query, new Object[]{id});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public void delete(String code){
		String query = "DELETE FROM bist_tour_bookings WHERE tour_booking_code = ?";
		try{
			jdbcTemplate.update(query, new Object[]{code});
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	@Override
	public TourBooking getTourBooking(Integer id){
		String query = "SELECT FROM bist_tour_bookings WHERE id = ?";
		try{
			TourBooking tourBooking  = jdbcTemplate.queryForObject(query, new Object[]{id}, new TourBookingMapper());
			return tourBooking;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	@Override
	public TourBooking getTourBooking(String code){
		String query = "SELECT FROM bist_tour_bookings WHERE tour_booking_code = ?";
		try{
			TourBooking tourBooking  = jdbcTemplate.queryForObject(query, new Object[]{code}, new TourBookingMapper());
			return tourBooking;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	@Override
	public List<TourBooking> list(){
		String query = "SELECT * FROM bist_tour_booking";
		try{
			List<TourBooking> tourBooking = jdbcTemplate.query(query, new TourBookingMapper());
			return tourBooking;
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
}
